package com.stone.front.order.controller;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.stone.back.dish.entity.BfCombo;
import com.stone.back.dish.entity.BfDish;
import com.stone.back.dish.entity.BfWeekmenu;
import com.stone.back.dish.entity.BfWeekmenuCombo;
import com.stone.back.order.entity.BfOrder;
import com.stone.back.order.entity.BfOrderInfo;
import com.stone.back.order.service.IOrderService;
import com.stone.back.user.entity.BfUser;
import com.stone.base.controller.BaseController;
import com.stone.base.entity.Page;
import com.stone.common.util.DateUtil;
import com.stone.common.util.RandomUtil;
import com.stone.front.dish.entity.Wares;
import com.stone.front.dish.service.IWaresFrontService;
import com.stone.front.order.entity.BfSite;
import com.stone.front.order.entity.PayConfig;
import com.stone.front.order.entity.ShopCar;
import com.stone.front.order.service.IOrderFrontService;
import com.stone.front.order.service.ISiteService;
import java.util.Collections;
import java.util.Map.Entry;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * 订单管理
 * @author Stone P
 *
 */
@Controller
@RequestMapping("/orderFrontCtl")
public class OrderFrontController extends BaseController {
	@Autowired
	private IOrderFrontService orderFrontService;
    @Autowired
    private IOrderService orderService;
	@Autowired
	private IWaresFrontService waresFrontService;
	@Autowired
	private ISiteService siteService;
	
	/**
	 * 加入购物车
	 */
	@RequestMapping("/addShopCar")
	public @ResponseBody String addShopCar(int waresId, int waresType, int waresCount, HttpServletRequest request) {
		//获取购物车数据
		Object shopCarObj = request.getSession().getAttribute(this.SHOPCAR);
		List<Wares> shopCarList = new ArrayList<Wares>();
		if(null != shopCarObj) {
			shopCarList = (List<Wares>) shopCarObj;
		}
		boolean exists = false;
		float shopCarTotalPrice = 0;
		//该商品是否已经在购物车中
		for (Wares wares : shopCarList) {
			if(waresId == wares.getWaresId() && waresType == wares.getType()) {
				//更新购物车中该商品的数量和总价
				wares.setCount(waresCount);
				wares.setTotalPrice(wares.getPrice() * waresCount);
				exists = true;
			}
			shopCarTotalPrice += wares.getTotalPrice();
		}
		if(false == exists) {
			//封装商品信息并加入购物车
			Wares wares = new Wares();
			wares.setType(waresType);
			wares.setWaresId(waresId);
			wares.setCount(waresCount);
			if(1 == waresType) {
				BfDish dish = this.waresFrontService.selectDishById(waresId);
				wares.setChName(dish.getDishchname());
				wares.setEnName(dish.getDishenname());
				wares.setPrice(dish.getPrice());
				wares.setPhoto(dish.getPhoto());
				//计算该商品总价
				float totalPrice = dish.getPrice() * waresCount;
				wares.setTotalPrice(totalPrice);
				shopCarTotalPrice += wares.getTotalPrice();
			} else {
				BfCombo combo = this.waresFrontService.selectComboById(waresId);
				//获取套餐中菜品名称字符串
				String[] dishNameArray = waresFrontService.selectComboDishStr(waresId);
				wares.setChName(dishNameArray[0]);
				wares.setEnName(dishNameArray[1]);
				wares.setPrice(combo.getPrice());
				wares.setPhoto(combo.getPhoto());
				//计算该商品总价
				float totalPrice = combo.getPrice() * waresCount;
				wares.setTotalPrice(totalPrice);
				shopCarTotalPrice += wares.getTotalPrice();
			}
			shopCarList.add(wares);
		}
		request.getSession().setAttribute(this.SHOPCARPRICE, shopCarTotalPrice);
		request.getSession().setAttribute(this.SHOPCAR, shopCarList);
		return shopCarList.size() + "," + shopCarTotalPrice;
	}
	
	/**
	 * 修改购物车商品数量
	 */
	@RequestMapping("/updateShopCar")
	public @ResponseBody Float updateShopCar(int waresId, int waresType, int waresCount, HttpServletRequest request) {
		Object shopCarPriceObj = request.getSession().getAttribute(this.SHOPCARPRICE);
		float shopCarPrice = 0;
		if(null != shopCarPriceObj) {
			shopCarPrice = Float.parseFloat(shopCarPriceObj.toString());
		}
		//获取购物车数据
		Object shopCarObj = request.getSession().getAttribute(this.SHOPCAR);
		List<Wares> shopCarList = new ArrayList<Wares>();
		if(null != shopCarObj) {
			shopCarList = (List<Wares>) shopCarObj;
		}
		boolean exists = false;
		float shopCarTotalPrice = 0;
		float waresUpdateTotalPrice = 0;
		//该商品是否已经在购物车中
		for (Wares wares : shopCarList) {
			if(waresId == wares.getWaresId() && waresType == wares.getType()) {
				if(0 == waresCount) {
					waresUpdateTotalPrice = -wares.getTotalPrice();
					shopCarList.remove(wares);
				} else {
					waresUpdateTotalPrice = (waresCount - wares.getCount()) * wares.getPrice();
					//更新购物车中该商品的数量和总价
					wares.setCount(waresCount);
					wares.setTotalPrice(wares.getPrice() * waresCount);
				}
				break;
			}
			shopCarTotalPrice += wares.getTotalPrice();
		}
//		BfOrder order = (BfOrder) request.getSession().getAttribute(this.ORDER);
		//更新总价
//		order.setTotalprice(order.getTotalprice() + waresUpdateTotalPrice);
//		map.put("orderTotalPirce", order.getTotalprice());
		shopCarPrice += waresUpdateTotalPrice;
		if(0 >= shopCarPrice) {
			shopCarPrice = 0;
		}
		request.getSession().setAttribute(this.SHOPCARPRICE, shopCarPrice);
		return shopCarPrice;
	}

	
	/**
	 * 加入购物车
	 */
	@RequestMapping("/addBusinessShopCar")
	public @ResponseBody String addBusinessShopCar(int[] waresIds, int[] waresTypes, int[] waresCounts, HttpServletRequest request) {
		//获取购物车数据
		Object shopCarObj = request.getSession().getAttribute(this.SHOPCAR);
		List<Wares> shopCarList = new ArrayList<Wares>();
		if(null != shopCarObj) {
			shopCarList = (List<Wares>) shopCarObj;
		}
		float shopCarTotalPrice = 0;
		for (int i = 0; i < waresIds.length; i++) {
			int type = 0;
			int waresId = waresIds[i];
			int waresType = waresTypes[i];
			int waresCount = waresCounts[i];
			Wares deleteWares = null;
			//该商品是否已经在购物车中
			for (Wares shopCarWares : shopCarList) {
				if(waresId == shopCarWares.getWaresId() && waresType == shopCarWares.getType()) {
					//更新购物车中该商品的数量和总价
					if(0 == waresCount) {
						deleteWares = shopCarWares;
						type = 2;
					} else {
						shopCarWares.setCount(shopCarWares.getCount() + waresCount);
						float price = shopCarWares.getPrice() * waresCount;
						shopCarWares.setTotalPrice(shopCarWares.getTotalPrice() + price);
						shopCarTotalPrice += shopCarWares.getTotalPrice();
						type = 1;
					}
					break;
				}
			}
			if(0 == type && 0 < waresCount) {
				//封装商品信息并加入购物车
				Wares addWares = new Wares();
				addWares.setType(waresType);
				addWares.setWaresId(waresId);
				addWares.setCount(waresCount);
				if(1 == waresType) {
					BfDish dish = this.waresFrontService.selectDishById(waresId);
					addWares.setChName(dish.getDishchname());
					addWares.setEnName(dish.getDishenname());
					addWares.setPrice(dish.getPrice());
					addWares.setPhoto(dish.getPhoto());
					//计算该商品总价
					float totalPrice = dish.getPrice() * waresCount;
					addWares.setTotalPrice(totalPrice);
					shopCarTotalPrice += addWares.getTotalPrice();
				} else {
					BfCombo combo = this.waresFrontService.selectComboById(waresId);
					//获取套餐中菜品名称字符串
					String[] dishNameArray = waresFrontService.selectComboDishStr(waresId);
					addWares.setChName(dishNameArray[0]);
					addWares.setEnName(dishNameArray[1]);
					addWares.setPrice(combo.getPrice());
					addWares.setPhoto(combo.getPhoto());
					//计算该商品总价
					float totalPrice = combo.getPrice() * waresCount;
					addWares.setTotalPrice(totalPrice);
					shopCarTotalPrice += addWares.getTotalPrice();
				}
				shopCarList.add(addWares);
			} else if(2 == type && null != deleteWares) {
				//商品数量为0删除购物车内该商品信息
				shopCarList.remove(deleteWares);
			}
		}
		request.getSession().setAttribute(this.SHOPCARPRICE, shopCarTotalPrice);
		request.getSession().setAttribute(this.SHOPCAR, shopCarList);
		return shopCarList.size() + "," + shopCarTotalPrice;
	}
	
	/**
	 * 填写订单联系信息
	 * @param order			订单信息
	 * @param request
	 * @return
	 */
	@RequestMapping("/addOrder")
	public ModelAndView commitOrder(BfOrder order, HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();
		if(StringUtils.isBlank(order.getUsername())) {
			mv.setViewName("/front/orderinput");
			return mv;
		}
		//获取购物车信息
		Object shopCarObj = request.getSession().getAttribute(this.SHOPCAR);
		List<Wares> shopCar = new ArrayList<Wares>();
		if(null == shopCarObj) {
			//购物车中没有商品
			mv.setViewName("redirect:/waresFrontCtl/menuInfo");
		} else {
			shopCar = (List<Wares>) shopCarObj;
			//购物车中是否有商品
			if(0 == shopCar.size()) {
				//购物车中没有商品
				mv.setViewName("redirect:/waresFrontCtl/menuInfo");
			} else {
				mv.setViewName("/front/orderinfo");
				if(StringUtils.isBlank(order.getOrderid())) {
					String orderId = "BF" + RandomUtil.getRandom16();
					order.setOrderid(orderId);
				}
				//计算订单总价和总数
				float totalPrice = 0;
				int waresCount = 0;
				for (Wares wares : shopCar) {
					totalPrice += wares.getTotalPrice();
					waresCount += wares.getCount();
				}
				order.setTotalprice(totalPrice);
				order.setWarescount(waresCount);
				//如果是已登录用户记录创建人Id
				BfUser user = new BfUser();
				Object userObj = this.getFrontCurrentUser(request);
				if(null != userObj) {
					user = (BfUser) userObj;
					order.setUserid(user.getUserid());
				}
				request.getSession().setAttribute(this.ORDER, order);
				//防止重复提交
				String orderSubmitMark = RandomStringUtils.randomAlphanumeric(16);
				request.getSession().setAttribute(this.ORDERSUBMITMARK, orderSubmitMark);
			}
		}
		return mv;
	}
	
	/**
	 * 访问订单详情页
	 * @return
	 */
	@RequestMapping("/orderinfo")
        public ModelAndView orderInfo(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView("/front/orderinfo");
		BfUser user = this.getFrontCurrentUser(request);
		if(null == user) {
			mv.setViewName("index");
			return mv;
		}
		//联系地址信息
		List<BfSite> siteList = this.siteService.selectSiteList(user.getUserid());
		mv.addObject("siteList", siteList);
		//防止重复提交
		String orderSubmitMark = RandomStringUtils.randomAlphanumeric(16);
		request.getSession().setAttribute(this.ORDERSUBMITMARK, orderSubmitMark);
		return mv;
	}
	
	/**
	 * 提交订单联系信息
	 * @param order			订单信息
	 * @param request
	 * @return
	 */
	@RequestMapping("/commitOrder")
	public ModelAndView commitOrderInfo(String orderId, String orderSubmitMark, HttpServletRequest request) {
		ModelAndView mv = new ModelAndView("/front/orderdisplay");
		//重复提交标示
		boolean repeat = false;
		Object orderSubmitMarkObj = request.getSession().getAttribute(this.ORDERSUBMITMARK);
		Object orderObj = request.getSession().getAttribute(this.ORDER);
		if(null != orderSubmitMarkObj) {
			if(!orderSubmitMarkObj.toString().equals(orderSubmitMark)) {
				//重复提交
				repeat = true;
			} 
		} else {
			if(null == orderObj) {
				//没有订单信息
				mv.setViewName("/front/orderinput");
				return mv;
			} else{
				repeat = true;
			}
		}
		if(true == repeat) {
			mv.addObject("orderId", orderId);
			//重复提交
			return mv;
		} else {
			//删除标示
			request.getSession().removeAttribute(this.ORDERSUBMITMARK);
			//保存订单信息
			BfOrder order = (BfOrder) orderObj;
			if(null != order) {
				//下单时间
				order.setCreatedate(new Date());
				//保存订单
				this.orderFrontService.addOrder(order);
				//保存订单商品详情
				List<Wares> waresList = (List<Wares>) request.getSession().getAttribute(this.SHOPCAR);
				for (Wares wares : waresList) {
					BfOrderInfo orderInfo = new BfOrderInfo();
					orderInfo.setOrderid(orderId);
					orderInfo.setDataid(wares.getWaresId());
					orderInfo.setDatacount(wares.getCount());
					orderInfo.setDatatype(wares.getType());
					this.orderFrontService.addOrderInfo(orderInfo);
				}
				//删除订单信息
				request.getSession().removeAttribute(this.ORDER);
				//删除购物车信息
				request.getSession().removeAttribute(this.SHOPCAR);
				mv.addObject("orderId", orderId);
				//****************************************************支付
//				this.fileParamter(mv, order);
//				mv.setViewName("redirect:https://www.99bill.com/gateway/recvMerchantInfoAction.htm");
			} else {
				//订单信息为空
				mv.setViewName("/front/orderinput");
			}
		}
		return mv;
	}
	
	/**
	 * 个人套餐订餐
	 * @return
	 */
	@RequestMapping("/toPersionOrderCombo")
	public ModelAndView toPersionOrderCombo() {
		ModelAndView mv = new ModelAndView("/front/persionorder");
		BfWeekmenu weekMenu = waresFrontService.selectWeekMenu();
		mv.addObject("waresPrice", weekMenu.getPrice());
		mv.addObject("waresType", 2);
		mv.addObject("orderType", 0);
		mv.addObject("waresId", 0);
		return mv;
	}
	
	/**
	 * 个人商品订餐
	 * @return
	 */
	@RequestMapping("/toPersionOrderDish")
	public ModelAndView toPersionOrderDish(int waresId, int waresType, HttpServletRequest request) {
		ModelAndView mv = new ModelAndView("/front/persionorder");
		float price = 0;
		if(1 == waresType) {
			BfDish dish = this.waresFrontService.selectDishById(waresId);
			price = dish.getPrice();
		} else {
			BfCombo combo = this.waresFrontService.selectComboById(waresId);
			price = combo.getPrice();
		}
		mv.addObject("waresPrice", price);
		mv.addObject("waresType", 1);
		mv.addObject("orderType", 1);
		mv.addObject("waresId", waresId);
		return mv;
	}

	/**
	 * 个人订餐
	 * @param startDate			配送起始时间
	 * @param endDate			配送结束时间
	 * @param orderType			订单类型(0：按日期购买套餐，1：购买商品)
	 * @param waresType			商品类型(1：普通商品，2：套餐)
	 * @param orderDayCount		订餐天数
	 * @param remark			备注
	 * @param request
	 * @return
	 */
	@RequestMapping("/addPersionShopCar")
	public @ResponseBody boolean persionOrderWares(Date startDate, Date endDate, int orderType, int waresId, int waresType, int orderDayCount, String remark, HttpServletRequest request) {
		//购物车
		Object persionShopCarObj = request.getSession().getAttribute(this.PERSIONSHOPCAR);
		float shopCarTotalPirce = 0;
		int waresCount = 0;
		ShopCar persionShopCar = new ShopCar();
		if(null != persionShopCarObj) {
			persionShopCar = (ShopCar) persionShopCarObj;
			shopCarTotalPirce = persionShopCar.getTotalPrice();
			waresCount = persionShopCar.getWaresCount();
		}
		if(0 == orderType) {
			//优惠价格
			int privilegeMoney = 0;
			if(orderDayCount >= 5) {
				//订餐天数大于5天优惠3块
				privilegeMoney = 3;
			}
			//已订商品
			Map<Integer, Wares> shopCarMap = new HashMap<Integer, Wares>();
			//本周套餐
			List<BfWeekmenuCombo> weekComboList = waresFrontService.selectWeekCombo();
			Map<Integer, Wares> weekComboMap = new HashMap<Integer, Wares>();
			for (BfWeekmenuCombo weekmenuCombo : weekComboList) {
				//商品使用菜单的价格
				weekmenuCombo.getCombo().setPrice(weekmenuCombo.getWeekMenu().getPrice());
				String[] waresName = waresFrontService.selectComboDishStr(weekmenuCombo.getCombo().getComboid());
				Wares wares = new Wares();
				wares.setWaresId(weekmenuCombo.getCombo().getComboid());
				wares.setChName(weekmenuCombo.getCombo().getCombochname() + " ：" + waresName[0]);
				wares.setEnName(weekmenuCombo.getCombo().getComboenname() + " ：" + waresName[1]);
				wares.setPhoto(weekmenuCombo.getCombo().getPhoto());
				wares.setPrice(weekmenuCombo.getCombo().getPrice() - privilegeMoney);
				wares.setType(2);
				wares.setStartDate(startDate);
				wares.setEndDate(endDate);
				wares.setRemark(remark);
				weekComboMap.put(weekmenuCombo.getSelldate(), wares);
			}
			//设置订餐起始日
			Calendar nowCalendar = Calendar.getInstance();
			nowCalendar.setTime(startDate);
			int nowWeekNum = nowCalendar.get(Calendar.DAY_OF_WEEK) - 1;
			//本周还剩几天
			int nowWeekCountDay = 6 - nowWeekNum;
			if(0 < orderDayCount) {
				int weekdaynum = nowWeekCountDay;
				if(orderDayCount <= nowWeekCountDay) {
					//订购天数小于本周剩余时间
					weekdaynum = orderDayCount;
				}
				//将本周的套餐加入购物车
				for (int i = nowWeekNum; i < nowWeekNum + weekdaynum; i++) {
					Wares wares = weekComboMap.get(i);
					wares.setCount(wares.getCount() + 1);
					wares.setTotalPrice(wares.getCount() * wares.getPrice());
					shopCarMap.put(wares.getWaresId(), wares);
				}
			}
			//订购日期超过本周
			if(orderDayCount > nowWeekCountDay) {
				//减去本周的天数
				orderDayCount -= nowWeekCountDay;
				//中间定了几个完整的周
				//完整的周数
				int weekCount = orderDayCount / 5;
				if(0 < weekCount) {
					for (BfWeekmenuCombo weekCombo : weekComboList) {
						if(shopCarMap.containsKey(weekCombo.getCombo().getComboid())) {
							//更新商品数量(增加的数量等于周数)
							Wares wares = shopCarMap.get(weekCombo.getCombo().getComboid());
							wares.setCount(wares.getCount() + weekCount);
							wares.setTotalPrice(wares.getPrice() * wares.getCount());
						} else {
							Wares wares = weekComboMap.get(weekCombo.getSelldate());
							wares.setCount(weekCount);
							wares.setTotalPrice(weekCount * wares.getPrice());
							shopCarMap.put(weekCombo.getCombo().getComboid(), wares);
						}
					}
				}
				//最后一周定了几天
				int lastWeekCount = orderDayCount % 5;
				for (int i = 1; i <= lastWeekCount; i++) {
					Wares wares = weekComboMap.get(i);
					if(shopCarMap.containsKey(wares.getWaresId())) {
						wares.setCount(wares.getCount() + 1);
						wares.setTotalPrice(wares.getTotalPrice() + wares.getPrice());
					} else {
						wares.setCount(wares.getCount() + 1);
						wares.setTotalPrice(wares.getCount() * wares.getPrice());
						shopCarMap.put(wares.getWaresId(), wares);
					}
				}
			}
			//更新购物车中的信息
			for (Entry<Integer, Wares> waresEntry : shopCarMap.entrySet()) {
				Wares wares = waresEntry.getValue();
				//购物车中不存在则加入
				persionShopCar.getWaresList().add(wares);
				shopCarTotalPirce += wares.getTotalPrice();
				waresCount += wares.getCount();
			}
		} else {
			Wares wares = new Wares();
			if(1 == waresType) {
				BfDish dish = this.waresFrontService.selectDishById(waresId);
				wares.setWaresId(dish.getDishid());
				wares.setChName(dish.getDishchname());
				wares.setEnName(dish.getDishenname());
				wares.setPhoto(dish.getPhoto());
				wares.setPrice(dish.getPrice());
			} else {
				BfCombo combo = this.waresFrontService.selectComboById(waresId);
				wares.setWaresId(combo.getComboid());
				wares.setChName(combo.getCombochname());
				wares.setEnName(combo.getComboenname());
				wares.setPhoto(combo.getPhoto());
				wares.setPrice(combo.getPrice());
			}
			wares.setCount(orderDayCount);
			wares.setTotalPrice(wares.getCount() * wares.getPrice());
			wares.setType(1);
			wares.setStartDate(startDate);
			wares.setEndDate(endDate);
			wares.setRemark(remark);
			//加入购物车
			persionShopCar.getWaresList().add(wares);
			shopCarTotalPirce += wares.getTotalPrice();
		}
		persionShopCar.setTotalPrice(shopCarTotalPirce);
		persionShopCar.setWaresCount(waresCount);
		request.getSession().setAttribute(this.PERSIONSHOPCAR, persionShopCar);
		return true;
	}

	/**
	 * 个人订餐详情页面
	 * @return
	 */
	@RequestMapping("/persionShopCar")
	public ModelAndView persionShopCar(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView("/front/persionshopcar");
		return mv;
	}

	/**
	 * 个人订餐提交订单
	 * @return
	 */
	@RequestMapping("/toPersionOrderCommit")
	public ModelAndView toPersionOrderCommit(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView("/front/persionordercommit");
		BfUser user = this.getFrontCurrentUser(request);
		if(null == user) {
			mv.setViewName("index");
			return mv;
		}
		//联系地址信息
		List<BfSite> siteList = this.siteService.selectSiteList(user.getUserid());
		mv.addObject("siteList", siteList);
		return mv;
	}

	/**
	 * 个人订餐提交订单
	 * @return
	 */
	@RequestMapping("/persionOrderCommit")
	public ModelAndView persionOrderCommit(String orderSubmitMark, int orderSiteId, HttpServletRequest request) {
		ModelAndView mv = new ModelAndView("/front/orderdisplay");
		BfUser user = this.getFrontCurrentUser(request);
		if(null == user) {
			mv.setViewName("index");
			return mv;
		}
		String orderId = "BF" + RandomUtil.getRandom16();
		//重复提交标示
		boolean repeat = false;
		Object orderSubmitMarkObj = request.getSession().getAttribute(this.ORDERSUBMITMARK);
		Object orderObj = request.getSession().getAttribute(this.ORDER);
		if(null != orderSubmitMarkObj) {
			if(!orderSubmitMarkObj.toString().equals(orderSubmitMark)) {
				//重复提交
				repeat = true;
			} 
		}
		if(true == repeat) {
			mv.addObject("orderId", orderId);
			//重复提交
			return mv;
		} else {
			//删除标示
			request.getSession().removeAttribute(this.ORDERSUBMITMARK);
			//购物车信息
			ShopCar shopCar = (ShopCar) request.getSession().getAttribute(this.PERSIONSHOPCAR);
			//保存订单信息
			BfSite site = siteService.selectSiteDetail(orderSiteId);
			BfOrder order = this.fillOrderSite(site);
			order.setOrderid(orderId);
			order.setEmail(user.getEmail());
			order.setUserid(user.getUserid());
			//商品总价
			order.setTotalprice(shopCar.getTotalPrice());
			//商品总量
			order.setWarescount(shopCar.getWaresCount());
			//下单时间
			order.setCreatedate(new Date());
			//保存订单
			this.orderFrontService.addOrder(order);
			//保存订单商品详情
			for (Wares wares : shopCar.getWaresList()) {
				BfOrderInfo orderInfo = new BfOrderInfo();
				orderInfo.setOrderid(orderId);
				orderInfo.setDataid(wares.getWaresId());
				orderInfo.setDatacount(wares.getCount());
				orderInfo.setDatatype(wares.getType());
				orderInfo.setStartdate(wares.getStartDate());
				orderInfo.setEnddate(wares.getEndDate());
				orderInfo.setRemark(wares.getRemark());
				this.orderFrontService.addOrderInfo(orderInfo);
			}
			//删除购物车信息
			request.getSession().removeAttribute(this.PERSIONSHOPCAR);
			mv.addObject("orderId", orderId);
		}
		return mv;
	}
	
	/**
	 * 填充订单配送信息
	 * @param site	地址信息
	 */
	private BfOrder fillOrderSite(BfSite site) {
		BfOrder order = new BfOrder();
		order.setSite(site.getSiteinfo());
		order.setUsername(site.getUsername());
		order.setPhone(site.getPhone());
		return order;
	}

	
	/**
	 * 商务商品订餐
	 * @return
	 */
	@RequestMapping("/toBusinessOrder")
	public ModelAndView toBusinessOrder(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView("/front/businessorder");
		return mv;
	}
	
    /**
     * 查询我的订单列表
     * @param request
     * @return 
     */
    @RequestMapping(value = "/api/order", method = RequestMethod.GET)
    public @ResponseBody Page queryMyOrders(int pageNumber, HttpServletRequest request) {
        BfUser user = this.getFrontCurrentUser(request);
        if(user == null)
            return new Page();
        return orderService.selectOrderByUserId(pageNumber, user.getUserid());
    }
    
    /**
     * 查询订单项
     */
    @RequestMapping(value = "/api/order/{oid}", method = RequestMethod.GET)
    public @ResponseBody List<BfOrderInfo> queryMyOrderItems(@PathVariable String oid, HttpServletRequest request) {
        BfUser user = this.getFrontCurrentUser(request);
        if(user == null)
            return Collections.EMPTY_LIST;
        return orderService.selectOrderInfoByOrderId(oid);
    }

    	
	/**
	 * 封装网关信息
	 * @param mv
	 * @param order
	 */
	private void fileParamter(ModelAndView mv, BfOrder order){
		PayConfig payConfig = new PayConfig();
		//字符集(UTF-8:1,GBK:2,GB2312:3)
		mv.addObject("inputCharset", "1");
		//人民币账号
		mv.addObject("merchantAcctId", payConfig.getCardNo());
		//网关版本(V2.0)
		mv.addObject("version", payConfig.getVersion());
		//网页显示诧言种类(1：中文)
		mv.addObject("language", "1");
		//签名类型(4)
		mv.addObject("signType", "1");
		//支付方式
		mv.addObject("payType", payConfig.getPayType());
		//商户订单号
		mv.addObject("orderId", order.getOrderid());
		//商户订单提交时间
		mv.addObject("orderTime", DateUtil.dateFormatStr(DateUtil.DATETIMESTR_FORMAT, order.getCreatedate()));
		//商户订单金额(分为单位,10.55元=1055)
		int totalPrice = (int) (order.getTotalprice() * 100);
		mv.addObject("orderAmount", totalPrice);
		//商品数量
		mv.addObject("productNum", order.getWarescount());
		//签名字符串
		mv.addObject("signMsg", payConfig.getSignMsg());
		//禁止重复提交
		mv.addObject("redoFlag", "1");
		mv.addObject("pageUrl", "http://www.baidu.com");
		mv.addObject("bgUrl", "http://www.163.com");
	}
	
}